/*
	* Arena Storage class

	* @namespace: Arena
	* @class: Arena.Storage

*/

(function(){
	Arena.Storage = function() {
        Arena.Storage.superclass.constructor.call(this);

        var supportLocalStorage;
        supportLocalStorage = function () {
            try {
                return 'localStorage' in window && window['localStorage'] !== null;
            } catch (e) {
                return false;
            }
        };
    };

    extend(Arena.Storage, Game.Storage,
	{
        player: null,
        playerId: 0,
        playerName: null,
        level: 0,
        score: 0,
        units: 0,
        health: 0,
        weapon: 0,

        // add new high score.
        // called when end game or game over
        // @param: keyScore: key of storage
        // @param: dataScore: data of storage
        addHighScore: function addHighScore(keyScore, dataScore){
            localStorage.setItem(keyScore, dataScore);
        },

        // render high score
        // return string value
        // @param: keyScore: key of storage
        renderHighScore: function renderHighScore(keyScore){
            localStorage.getItem(keyScore);
        },

        addPlayer: function addPlayer(playerId, playerName, level, health, units, weapon, score){
            this.player = new Array();

            this.playerId = playerId;
            this.playerName = playerName;
            this.level = level;
            this.health = health;
            this.units = units;
            this.weapon = weapon;
            this.score = score;

            this.player.push(playerId);
            this.player.push(playerName);
            this.player.push(level);
            this.player.push(health);
            this.player.push(units);
            this.player.push(weapon);
            this.player.push(score);

            return this.player;
        },

        // save state of player when pause game or out game but not game over
        // state of game: player name, level, health, weapon, score, remain unit
        // add player[] array in storage
        savePlayerState: function savePlayerState(keyPs, player){
            this.playerId = localStorage["Arena.game.player.Id"];
            this.playerName = localStorage["Arena.game.player.Name"];
            this.level = localStorage["Arena.game.player.Level"];
            this.health = localStorage["Arena.game.player.Health"];
            this.weapon = localStorage["Arena.game.player.Weapon"];
            this.score = localStorage["Arena.game.player.CurrentScore"];
            this.units = localStorage["Arena.game.player.remainUnit"];

            player = this.addPlayer(this.playerId,this.playerName,this.level,this.health, this.units,this.weapon,this.score);

            localStorage.setItem(keyPs, player);
        },

		onRender: function onRender(cxt){
		},
		
		// render menu game: new game - resume - High score - Settings - How to play (help) - About
		sceneRendererMenu: function sceneRendererMenu(ctx)
      {
		 var txtNewGame = "New game";
		 var txtResumeGame = "Resume game";
		 var txtSetting = "Settings";
		 var txtHighScore = "High score";
		 var txtHelper = "Helps";
		 var textAbout = "About";
		 
		 var ypos = ~~(150 * this.world.scale * 2);
         ctx.fillStyle = "white";
         ctx.font = ~~(20 * this.world.scale * 2) + "pt Arial";
         ctx.fillText(txtNewGame, (GameHandler.width - ctx.measureText(txtNewGame).width) / 2, ypos);
		 /*if(localStorage["Arena.game.in.progress"]=="true"){*/
         ypos += ~~(40  * this.world.scale * 2);
         ctx.fillText(txtResumeGame, (GameHandler.width - ctx.measureText(txtResumeGame).width) / 2, ypos);
		 /*}*/
		 ypos += ~~(40  * this.world.scale * 2);
         ctx.fillText(txtSetting, (GameHandler.width - ctx.measureText(txtSetting).width) / 2, ypos);
		 ypos += ~~(40  * this.world.scale * 2);
         ctx.fillText(txtHighScore, (GameHandler.width - ctx.measureText(txtHighScore).width) / 2, ypos);
		 ypos += ~~(40  * this.world.scale * 2);
         ctx.fillText(txtHelper, (GameHandler.width - ctx.measureText(txtHelper).width) / 2, ypos);
		 ypos += ~~(40  * this.world.scale * 2);
         ctx.fillText(textAbout, (GameHandler.width - ctx.measureText(textAbout).width) / 2, ypos);
      },
		
		// render high score
		sceneRenderScore: function sceneRenderScore(ctx){
			var ypos = ~~(150 * this.world.scale * 2);
			ctx.fillStyle = "white";
			ctx.font = ~~(24 * this.world.scale * 2) + "pt Arial";
			
			var txtTextScore = "High Score";
			ctx.fillText(txtTextScore, (GameHandler.width - ctx.measureText(txtTextScore)) / 2, ypos);
			
			var txtHighScore = this.renderHighScore(SCOREDBKEY);
			ctx.font = ~~(48 * this.world.scale * 2) + "pt Arial";
			for(var i=0; i=8-txtHighScore.length;++i){
				txtHighScore = "0" + txtHighScore;
			}
			ctx.fillText(txtHighScore, (GameHandler.width - ctx.measureText(txtHighScore)) /2 , ypos);
			
		},
		
		// render settings game: choice colour of player,...
		sceneRenderSetting: function sceneRenderSetting(ctx){
		},
		
		// render information: about us
		sceneRenderAbout: function sceneRenderAbout(ctx){
		 var ypos = ~~(150 * this.world.scale * 2);
         ctx.fillStyle = "white";
         ctx.font = ~~(24 * this.world.scale * 2) + "pt Arial";
         ctx.fillText("About us", GameHandler.width * 0.2, ypos);
         ctx.font = ~~(12 * this.world.scale * 2) + "pt Arial";
         ypos += ~~(32 * this.world.scale * 2);
         ctx.fillText("We are Casual Game HTML5 Team", GameHandler.width * 0.15, ypos);
		},
		
		// render information: how to play
		sceneRenderHelp: function sceneRenderHelp(ctx){
		 var ypos = ~~(150 * this.world.scale * 2);
         ctx.fillStyle = "white";
         ctx.font = ~~(24 * this.world.scale * 2) + "pt Arial";
         ctx.fillText("How to play...", GameHandler.width * 0.2, ypos);
         ctx.font = ~~(12 * this.world.scale * 2) + "pt Arial";
         ypos += ~~(32 * this.world.scale * 2);
         ctx.fillText("Use the WASD or cursor keys to move your ship.", GameHandler.width * 0.15, ypos);
         ypos += ~~(20 * this.world.scale * 2);
         ctx.fillText("Use the mouse to direct your automatic weapons.", GameHandler.width * 0.15, ypos);
         ypos += ~~(20 * this.world.scale * 2);
         ctx.fillText("Dodge the enemy ships that will try to destroy you!", GameHandler.width * 0.15, ypos);
         ypos += ~~(20 * this.world.scale * 2);
         ctx.fillText("Pickup the score multipliers dropped by enemies.", GameHandler.width * 0.15, ypos);
		}
	});
})();